package zym.diandian.module.popup;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;

import zym.diandian.R;

public class ColorPicker extends Dialog implements SeekBar.OnSeekBarChangeListener {

    private int b;

    private int g;

    private int mColor;

    private int r;

    private View view_color;

    private int width, height;

    public ColorPicker(Context context, @NonNull int color, int width, int height) {
        super(context);
        this.mColor = color;
        this.width = width;
        this.height = height;
    }

    /**
     * 返回选择的颜色
     *
     * @return
     */
    public int getColor() {
        return Color.rgb(this.r, this.g, this.b);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.setContentView(R.layout.layout_colorpicker);

        this.view_color = this.findViewById(R.id.viewColor);
        this.view_color.setBackgroundColor(this.mColor);

        SeekBar red = (SeekBar) this.findViewById(R.id.red);
        SeekBar green = (SeekBar) this.findViewById(R.id.green);
        SeekBar blue = (SeekBar) this.findViewById(R.id.blue);

        red.setOnSeekBarChangeListener(this);
        green.setOnSeekBarChangeListener(this);
        blue.setOnSeekBarChangeListener(this);

        WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
        attributes.width = (width / 4) * 3;
        attributes.height = (height/ 4) * 3;
        attributes.gravity = Gravity.CENTER;
        this.getWindow().setAttributes(attributes);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.red:
                this.r = progress;
                break;
            case R.id.green:
                this.g = progress;
                break;
            case R.id.blue:
                this.b = progress;
                break;
            default:
                break;
        }

        this.view_color.setBackgroundColor(Color.rgb(this.r, this.g, this.b));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
